<?php

class Admin_NewsController extends Zend_Controller_Action

{

    protected $_page =1;

    

    protected $_arrParam;

    

    protected $_actionMain;

    

    public function init(){

        $this->_arrParam = $this->_request->getParams();

	$this->_page = $this->_arrParam['page'];

        $this->_actionMain = '/' . $this->_arrParam['module'] 

							 . '/' . $this->_arrParam['controller']	. '/index/page/'.$this->_page;

    }

    

    public function indexAction()

    {

        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_NEWS_INDEX");

        $this->view->headTitle($this->view->title);

        $newsModel = new Admin_Model_News();

        $listNews = $newsModel->listNews();

        $paginator = Zend_Paginator::factory($listNews);

        $paginator->setItemCountPerPage(NUM_PER_PAGE);

        $paginator->setCurrentPageNumber($this->_page);

        $this->view->page = $this->_page;

        $this->view->paginator = $paginator;

    }

    

    public function activeAction(){

        $this->_helper->layout()->disableLayout();

        $type = $this->_request->getParam('type',1);

        $id = $this->_request->getParam('id',0);

        if($type>0){

                $type = 1;

        }else{

                $type = 0;

        }

        $modelNews = new Admin_Model_News();

        $modelNews->update(array('active'=>$type), 'id='.$id);

        $this->_redirect($this->_actionMain);

    }

    

    public function slideAction(){

        $this->_helper->layout()->disableLayout();

        $type = $this->_request->getParam('type',1);

        $id = $this->_request->getParam('id',0);

        if($type>0){

                $type = 1;

        }else{

                $type = 0;

        }

        $modelNews = new Admin_Model_News();

        $modelNews->update(array('slide'=>$type), 'id='.$id);

        $this->_redirect($this->_actionMain);

    }

	

    public function insertAction()

    {

        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_NEWS_INSERT");

        $this->view->headTitle($this->view->title);

        if($this->_request->isPost()){

                $title = trim($this->_arrParam['title']);

				$title_id = strtolower(preg_replace('/[^a-zA-Z0-9 ]/s', '', trim($this->_arrParam['title_id'])));	

				//name id
	    		$title_id= str_replace(' ', '-', trim($title_id));

                $short_des = $this->_arrParam['short_des'];

                $content = $this->_arrParam['content'];

                $source_image = $this->_arrParam['source_image'];

                $sys_image = $this->_arrParam['sys_image'];

                $active = $this->_arrParam['active'];

                $slide = $this->_arrParam['slide'];



                if (! Zend_Validate::is($title, 'NotEmpty')) $messages[] = array(

                        'text' => "Tên: Không được để trống", 

                        'type' => 'errormsg',

                );



                if (! Zend_Validate::is($title_id, 'NotEmpty')) $messages[] = array(

                        'text' => "Tiêu đề id: Không được để trống", 

                        'type' => 'errormsg',

                );



                if (! Zend_Validate::is($short_des, 'NotEmpty')) $messages[] = array(

                        'text' => "Giới thiệu: Không được để trống", 

                        'type' => 'errormsg',

                );

                

                if (! Zend_Validate::is($content, 'NotEmpty')) $messages[] = array(

                        'text' => "Nội dung: Không được để trống", 

                        'type' => 'errormsg',

                );



                if(count($messages)==0){

                        $title_id = str_replace(':','',$title_id);

                        $nameid = strtoupper($title_id);

                        $modelNews = new Admin_Model_News();

                        $date = new Zend_Date(); 

                        $data = array('title'=>$title, 

                                        'title_id'=>$title_id,

                                        'short_des'=>$short_des, 

                                        'content'=>$content, 

                                        'source_image'=>$source_image, 

                                        'sys_image'=>$sys_image,

                                        'active' =>$active,

                                        'slide'=>$slide,

                                        'date'=>$date->toString(Zend_Date::TIMESTAMP));

                        $id = $modelNews->insert($data);

                        $messages[] = array(

                                        'text' => "Đã thêm game mới thành công", 

                                        'type' => 'success',);	

                }



                $this->view->title = $title;			

                $this->view->title_id = $title_id;

                $this->view->short_des = $short_des;

                $this->view->content = $content;

                $this->view->source_image = $source_image;

                $this->view->sys_image = $sys_image;

                $this->view->active = $active;

                $this->view->slide = $slide;

                $this->view->message = $messages;

        }

    }

    

    public function updateAction()

	{

		$translate = Zend_Registry::get('Zend_Translate');

		$this->view->title = $translate->_("TITLE_ADMIN_NEWS_UPDATE");

		$this->view->headTitle($this->view->title);

		$modelNews = new Admin_Model_News();

		$id_news = $this->_getParam('id', 0);

		if($this->_request->isPost()){

                    $title = trim($this->_arrParam['title']);

                    $title_id = trim($this->_arrParam['title_id']);

                    $short_des = $this->_arrParam['short_des'];

                    $content = $this->_arrParam['content'];

                    $source_image = $this->_arrParam['source_image'];

                    $sys_image = $this->_arrParam['sys_image'];

                    $active = $this->_arrParam['active'];

                    $slide = $this->_arrParam['slide'];



                    if (! Zend_Validate::is($title, 'NotEmpty')) $messages[] = array(

                            'text' => "Tên: Không được để trống", 

                            'type' => 'errormsg',

                    );



                    if (! Zend_Validate::is($title_id, 'NotEmpty')) $messages[] = array(

                            'text' => "Tiêu đề id: Không được để trống", 

                            'type' => 'errormsg',

                    );



                    if (! Zend_Validate::is($short_des, 'NotEmpty')) $messages[] = array(

                            'text' => "Giới thiệu: Không được để trống", 

                            'type' => 'errormsg',

                    );



                    if (! Zend_Validate::is($content, 'NotEmpty')) $messages[] = array(

                            'text' => "Nội dung: Không được để trống", 

                            'type' => 'errormsg',

                    );

			

                    if(count($messages)==0){

                        $title_id = str_replace(':','',$title_id);

                        $nameid = strtoupper($title_id);

                        $modelNews = new Admin_Model_News();

                        $date = new Zend_Date(); 

                        $data = array('title'=>$title, 

                                        'title_id'=>$title_id,

                                        'short_des'=>$short_des, 

                                        'content'=>$content, 

                                        'source_image'=>$source_image, 

                                        'sys_image'=>$sys_image,

                                        'active' =>$active,

                                        'slide'=>$slide);

                        $modelNews->update($data, 'id='.$id_news);

                        $messages[] = array('text' => "Đã update thành công", 

                                            'type' => 'success',);	

                    }

			

                    $this->view->title = $title;			

                    $this->view->title_id = $title_id;

                    $this->view->short_des = $short_des;

                    $this->view->content = $content;

                    $this->view->source_image = $source_image;

                    $this->view->sys_image = $sys_image;

                    $this->view->active = $active;

                    $this->view->slide = $slide;

                    $this->view->message = $messages;

		}else{

			$data = $modelNews->fetchRow('id='.$id_news);

			$data = $data->toArray();

                        $this->view->title = $data['title'];	

                        $this->view->title_id = $data['title_id'];

                        $this->view->short_des = $data['short_des'];

                        $this->view->content = $data['content'];

                        $this->view->source_image = $data['source_image'];

                        $this->view->sys_image = $data['sys_image'];

                        $this->view->active = $data['active'];

                        $this->view->slide = $data['slide'];

		}

	}

    

    public function deleteAction()

    {

        $translate = Zend_Registry::get('Zend_Translate');

        $this->view->title = $translate->_("TITLE_ADMIN_NEWS_DELETE");

        $this->view->headTitle($this->view->title);

        if ($this->getRequest()->isPost()) {

            $del = $this->getRequest()->getPost('del');          

            if ($del == 'Yes') { 

                $id = $this->getRequest()->getPost('id'); 

                $newsModel = new Admin_Model_News();

                $result = $newsModel->delete('id='.$id);

            }

            $this->_redirect($this->_actionMain);			

        }else{

            $id = $this->_getParam('id', 0);

            $this->view->nid = $id;

        }

    }

        

}